/*
 * ComponentOrientation.java  0.4.0 / Oct 26, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.swing;

import java.util.Locale;

/**
 * ComponentOrientation Class.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public final class ComponentOrientation {

// Public Static Fields. -------------------------------------------------------

  public static final ComponentOrientation UNKNOWN;
  public static final ComponentOrientation LEFT_TO_RIGHT;
  public static final ComponentOrientation RIGHT_TO_LEFT;

// Private Static Fields. ------------------------------------------------------

  private static final int ORIENTATION_UNKNOWN = 1;
  private static final int ORIENTATION_HORIZONTAL = 2;
  private static final int ORIENTATION_LEFT_TO_RIGHT = 4;

// Static Initialization. ------------------------------------------------------

  static {
    UNKNOWN = new ComponentOrientation(ORIENTATION_UNKNOWN |
                                       ORIENTATION_HORIZONTAL |
                                       ORIENTATION_LEFT_TO_RIGHT);
    LEFT_TO_RIGHT = new ComponentOrientation(ORIENTATION_HORIZONTAL |
                                             ORIENTATION_LEFT_TO_RIGHT);
    RIGHT_TO_LEFT = new ComponentOrientation(ORIENTATION_HORIZONTAL);
  }

// Package-Private Fields. -----------------------------------------------------

  final int value;

// Private Constructor. --------------------------------------------------------

  private ComponentOrientation(int orientation) {
    this.value = orientation;
  }

// Public Methods. -------------------------------------------------------------

  /**
   * Returns <tt>true</tt> if orientation is horizontal.
   *
   * @return <tt>true</tt> if orientation is horizontal
   */
  public boolean isHorizontal() {
    return (value & ORIENTATION_HORIZONTAL) != 0;
  }

  /**
   * Returns <tt>true</tt> if orientation is left-to-right.
   *
   * @return <tt>true</tt> if orientation is left-to-right
   */
  public boolean isLeftToRight() {
    return (value & ORIENTATION_LEFT_TO_RIGHT) != 0;
  }

// Public Static Methods. ------------------------------------------------------

  /**
   * Returns component orientation for the specified locale object.
   *
   * @param locale the locale
   *
   * @return component orientation for the specified object
   */
  public static ComponentOrientation getOrientation(Locale locale) {
    if (locale == null) {
      return UNKNOWN;
    }
    String lang = locale.getLanguage();
    if ("ar".equals(lang) || "fa".equals(lang) || "iw".equals(lang) ||
        "ur".equals(lang)) {
      return RIGHT_TO_LEFT;
    }
    return LEFT_TO_RIGHT;
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  public int hashCode() {
    return value;
  }

  /**
   * {@inheritDoc}
   */
  public boolean equals(Object obj) {
    if (obj == this) {
      return true;
    }
    if ((obj != null) && (obj.getClass() == getClass())) {
      ComponentOrientation co = (ComponentOrientation)obj;
      return co.value == value;
    }
    return false;
  }

}
